package com.cheyun.netsalev3.view

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.DaoDianDetailsParam
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.order.OrderKeliuData
import com.cheyun.netsalev3.databinding.ActivityEditCustomStoreBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.viewmodel.EditCustomStoreActivityViewModel

class EditCustomStoreActivity : BaseActivity<ActivityEditCustomStoreBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_edit_custom_store
    }
    var firstEdit:Int = 0
    var scanData:ReserveInfo? = null
    override fun initViewModel(): ViewModel? {
        var param = intent.getSerializableExtra("data")
        if(intent.hasExtra("scanData")) {
            scanData = intent.getSerializableExtra("scanData") as ReserveInfo
        }

        return if (param == null) {
            //添加客流
            binding.headview.titleText?.text = "添加"
            var data = DaoDianDetailsParam()
            var factory = InjectorUtils.getEditCustomStoreViewModelFactory(data)
            ViewModelProviders.of(this@EditCustomStoreActivity, factory).get(EditCustomStoreActivityViewModel::class.java)
        } else {
            //编辑客流
            var data = param as DaoDianDetailsParam
            var factory = InjectorUtils.getEditCustomStoreViewModelFactory(data)
            firstEdit = 1
            ViewModelProviders.of(this@EditCustomStoreActivity, factory).get(EditCustomStoreActivityViewModel::class.java)
        }

        return null
    }

    override fun initData() {
//        if(scanData.isNotEmpty()){
//            binding.viewModel?.getScanResult(scanData)
//        }
        if(intent.hasExtra("hiddenbind")){
            var hiddenbind = intent.getBooleanExtra("hiddenbind",false)
            if(hiddenbind){
                binding.viewModel?.showDaodian?.postValue(false)
            }
        }
    }

    override fun initView() {

        binding.txtPhone.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (FunctionUtils.isPhone2(binding.txtPhone.text.toString(), this@EditCustomStoreActivity)) {
                    if(firstEdit == 1){
                        firstEdit ++
                        return
                    }
                    binding.viewModel?.getPhone(binding.txtPhone.text.toString(), this@EditCustomStoreActivity)
                }
            }
        })
        if(scanData!=null) {
            binding.viewModel?.setScanData(scanData!!)
            if(firstEdit == 0) {
                binding.llScan.visibility = View.VISIBLE
                binding.llNormal.visibility = View.GONE
            }
            binding.txtPhone.isEnabled = false
            binding.txtPhone.isFocusableInTouchMode = false
            binding.txtPhone.isFocusable = false
        }
        var loginData = MySharedPreferences.getLoginData()
        loginData?.member?.apps?.receive?.apply {
            for (i in this) {
                if (i == 3) {
                    binding.btnAddKeliu.visibility = View.VISIBLE
                }
                if (i == 6) {
                    binding.btnAddDrive.visibility = View.VISIBLE
                }
            }
        }
        if(scanData!=null){
            binding.btnAddDrive.text = "下一步"
            if(scanData?.qrtype.equals("adorder")){
                binding.btnAddDrive.visibility = View.GONE
            }

            if(scanData?.qrtype.equals("driving")){
                binding.btnAddKeliu.visibility = View.GONE
                binding.btnAddDrive.visibility = View.VISIBLE
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode==102){
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.setCarData(carBrandParam)
                binding.txtSeriesname.text = carBrandParam.seriesname
                binding.txtSpecname.text = carBrandParam.specname
            }
        }else if(resultCode == 101){
            data?.apply {
                var data = data?.getSerializableExtra("data") as OrderKeliuData
                binding.viewModel?.setDaoDaian(data)
                if(scanData==null) {
                    binding.txtUname.setText(data.uname)
                    binding.txtPhone.setText(data.phone)
                }
                binding.txtRcvname.text = data.rcvname
                binding.txtArrivetime.text = data.arrivetime
            }
        }
    }
}
